﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.Production;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.Production
{
    public class TechnologyServices : ITechnologyServices
    {
        public TechnologyServices(IBaseRepository conn)
        {
            Conn = conn;
        }

        public IBaseRepository Conn { get; }

        public async Task<bool> AddPro_Routes(pro_route pro)
        {
            bool bol = await Conn.InsertAsync(pro);

            return bol;
        }

        public async Task<bool> AddPro_Routes_Process(pro_route_process pro)
        {
            bool bol = await Conn.InsertAsync(pro);

            return bol;
        }

        public async Task<bool> DelPro_Routes(string Id)
        {
            //var sql = "delete pro_route where route_id=@Id";
            //var sqls = "delete pro_route where process_id=@Id";
            //bool bol = await Conn.DeleteAsync(sql, new { Id = Id });
            //await Conn.DeleteAsync(sqls, new { Id = Id });

            return true;
        }

        public async Task<IEnumerable<pro_process>> GetALLPro_Processes()
        {
            var sql = "select * from pro_process";

            var list = await Conn.QueryAllAsync<pro_process>(sql);

            return list;
        }

        public async Task<IEnumerable<pro_route>> GetPro_Routes(int PageIndex, int PageSize, string route_code, string route_name, string status)
        {
            var sql = "select * from pro_route where route_code like @route_code and route_name like @route_name and enable_flag like @status";

            sql += " order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";

            var list = await Conn.QueryAsync<pro_route>(sql, new { PageIndex, PageSize, route_code = "%" + route_code + "%", route_name = "%" + route_name + "%", status = "%" + status + "%" });

            return list;
        }

        public async Task<pro_route> GetPro_Routes(string Id)
        {
            var sql = "select * from pro_route where route_id=@Id";

            var data = await Conn.QueryFirstOrDefaultAsync<pro_route>(sql, new { Id = Id });

            return data;
        }

        public async Task<int> GetPro_RoutesCount(string route_code, string route_name, string status)
        {
            var sql = "select count(1) from pro_route where route_code like @route_code and route_name like @route_name and enable_flag like @status";
            var count = await Conn.GetRecoreCountAsync(sql, new { route_code = "%" + route_code + "%", route_name = "%" + route_name + "%", status = "%" + status + "%" });

            return count;
        }

        public async Task<IEnumerable<pro_route_process>> GetPro_Route_Processes(string Id)
        {
            var sql = "select * from pro_route_process where route_id=@Id order by order_num asc";

            var list = await Conn.QueryAsync<pro_route_process>(sql, new { Id = Id });

            return list;
        }

        public async Task<bool> UpdatePro_Route(pro_route pro)
        {
                bool bol = await Conn.UpdateAsync(pro, "route_id");

                return bol;
        }
    }
}
